<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1 {
            width: 100px;
            height: 100px;
            background: green;
            position: fixed;
            left: 100px;
            top: 100px;
        }

        .box2 {
            width: 100px;
            height: 100px;
            background: rgb(0, 45, 128);
            position: fixed;
            left: 400px;
            top: 400px;
        }

        .active {
            border: yellow solid 5px;
        }
    </style>
</head>

<body>
    <div class="box1"></div>
    <div class="box2"></div>
    <script>

        let box = null
        window.onclick = function (e) {
            // document.querySelector('.active')?.classList.remove('active');
            // e.target.classList.add('active');
            box = e.target;
        }
        window.onkeydown = function (e) {
            if (box === null) return;
            const pos = box.getBoundingClientRect();
            if (e.key === 'ArrowUp') {
                // 上
                box.style.top = pos.top - 10 + 'px';
            }
            if (e.key === 'ArrowDown') {
                // 下
                box.style.top = pos.top + 10 + 'px';
            }
            if (e.key === 'ArrowLeft') {
                // 左
                box.style.left = pos.left - 10 + 'px';
            }
            if (e.key === 'ArrowRight') {
                // 右
                box.style.left = pos.left + 10 + 'px';
            }

        }



    </script>
</body>

</html>